package com.zsc.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.shixun.entity.Message;
import com.zsc.shixun.mapper.MessageMapper;
import com.zsc.shixun.model.message.MessageMapping;
import com.zsc.shixun.model.message.MessageVo;
import com.zsc.shixun.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author ZWYZY
 * @since 2021/4/16
 */
@Service("messageService")
@Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    MessageMapping messageMapping;
    @Override
    public List<MessageVo> getMessageListByUserId(Long userId) {
        List<Message> messageList = this.list(new QueryWrapper<Message>().lambda().eq(Message::getUserId, userId).eq(Message::getIsDelete,false).orderByDesc(Message::getUpdateTime));
        return messageMapping.entitysToVos(messageList);
    }

    @Override
    public void updateActive(Long id,Boolean active) {
        Message message = this.getById(id);
        if(message!=null)
        {
            message.setIsActive(active);
            this.updateById(message);
        }
    }

    @Override
    public void updateAllActive(Long userId, Boolean active) {
        List<Message> messageList = this.list(new QueryWrapper<Message>().lambda().eq(Message::getUserId, userId));
        messageList.forEach(m->{
            m.setIsActive(false);
        });
        this.saveBatch(messageList);
    }

    @Override
    public void updateAllDelete(Long userId, Boolean delete) {
        List<Message> messageList = this.list(new QueryWrapper<Message>().lambda().eq(Message::getUserId, userId).eq(Message::getIsDelete,false));
        messageList.forEach(m->{
            m.setIsDelete(true);
        });
        this.updateBatchById(messageList);
    }

    @Override
    public MessageVo insertMessage(Long userId, Long sendId, String message) {
        Message m=new Message();
        m.setMessage(message);
        m.setUserId(userId);
        m.setSendId(sendId);
        this.save(m);

        return messageMapping.entityToVo(m);
    }

    @Override
    public void updateDeleteById(Long id) {
        System.out.println("执行修改");
        Message message = this.getById(id);
        if(!message.getIsDelete())
        {
            message.setIsDelete(true);
            this.updateById(message);
        }
//        Message message = this.getOne(new QueryWrapper<Message>().lambda().eq(Message::getId, id).eq(Message::getIsDelete, false));
//        if(message!=null)
//        {
//            this.updateById(message);
//        }
    }
}
